package com.hengpeng.itfints.controller.member;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.itfinbase.constants.RespCodeConstants;
import com.hengpeng.itfinbase.message.ResponseMessage;
import com.hengpeng.itfinbase.message.userInfo.UserInfoResponse;
import com.hengpeng.itfinbase.persist.UserInfo;
import com.hengpeng.itfinbase.persist.VerifyCode.VerifyType;
import com.hengpeng.itfints.controller.BaseController;
import com.hengpeng.itfints.tools.PatternUtils;

/**
 * 忘记密码控制类
 * @author lugaofei
 * 20151022
 */

@Controller
public class ForgetPasswordController extends BaseController {
    
    @RequestMapping(value = "/getBackPassword")
    @ResponseBody
    public ServiceResult<String> getBackPassword(HttpServletRequest request,String mobile,String mobilecode,String password){
        ServiceResult<String> result = new ServiceResult<String>();
        UserInfo userInfo = serviceFactory.getUserService().getUserByMobile(mobile);
        if(userInfo==null){
            return validateFailure("用户不存在");
        }
        int pleng = password.length();

        if (pleng < 6 || pleng > 16) {
            return validateFailure("密码字符长度应为6-16之间");
        }
        if (mobile == null) {
            return validateFailure("手机号码不能为空");
        } else {
            if (!PatternUtils.MOBILE_PATTERN.matcher(mobile).matches()) {
                return validateFailure("手机号码格式不正确");
            }
        }
        if (StringUtils.isEmpty(mobilecode)) {
            return validateFailure("短信验证码不能为空");
        }
        ResponseMessage response = serviceFactory.getUserService().doValidMobile(null, mobile, mobilecode, VerifyType.FOUNDLOGINPWD);
        if (response != null)
        {
            if (RespCodeConstants.CommonCode.TRANS_SUC.equals(response.getRespCode()))
            {
                UserInfoResponse responseUser = this.serviceFactory.getUserService().backPassword(mobile, password);
                if (responseUser != null)
                {
                    if (RespCodeConstants.CommonCode.TRANS_SUC.equals(responseUser.getRespCode()))
                    {
                        result.setIsSuccess(true);
                        result.setMessage("修改密码成功");
                        refreshSessionMember(request);
                    }
                    else
                    {
                        return validateFailure(responseUser.getRespDesc());
                    }
                }
                else
                {
                    return validateFailure("修改密码失败");
                }
            }
            else
            {
                return validateFailure(response.getRespDesc());
            }
        }
        else
        {
            return validateFailure("手机验证码错误");
        }
        return result;
    }
}
